﻿<?php

class Page {

	public $doctype;
	public $title;
	private $meta;
	private $externalCSS;
	private $externalScript;
	public $innerStyle;
	public $content;
	public $innerScript; 
	
	//생성자(contructor)
	public function __construct($title=""){ //$title=""이란 뜻은 $title값이 들어올 수도 있고 안들어올수도 있다.
		$this->doctype = "xhtml1.1";
		if ($title != ""){
			$this->title = $title;
		} else{
			$this->title = "";
		}
	}
	
	public function makeupPage($code){
		//DB에서 필요한 정보를 읽어오기
		@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );
		if ($db->connect_errno) {
			try{
				//예외를 발생시키는 부분
				throw new MyException("database connection Error", 1);
			} catch(MyException $e){
				//발생한 예외를 받아서 처리하는 부분
				$e->display();
			}
		}

		//encoding setup
		$db->set_charset("utf8");

		$query = "select * from pages where code = '" . $code . "'";
		$result = $db->query($query);
		if($result->num_rows==0){
			//에러 메시지
		}
		$num_results = $result->num_rows;     //result 객체의 num_rows 프로퍼티(행의 개수를 담고있는 프로퍼티)
		$row = $result->fetch_object();    //fetch_assoc는 한 행만 가져오는 메소드. 이 메소드의 리턴 타입은 name value의 배열.
		$this->title = $row->title;
		
		// 속성(어트리뷰트)에 값을 할당한다. 

		//externalCSS 배열 값 추가
		$arr_e_css = explode(' ', $row->e_css);
		foreach ($arr_e_css as $single){
			$this->addExternalCSS($single);
		}

		//externalScript 배열 값 추가
		$arr_e_javascript = explode(' ', $row->e_javascript);
		foreach ($arr_e_javascript as $single){
			$this->addExternalScript($single);
		}

		//innerScript와 innerStyle
		$this->innerScript = $row->i_javascript;
		$this->innerStyle = $row->i_css;

		//meta 배열 값 추가
		$this->addMeta("name", "author", $row->author);
		$this->addMeta("name", "keywords", $row->keywords);

		$query = "select * from menus order by order1, order2";
		$result = $db->query($query);

		//Menu
		$this->content = "<ul>\n";
		for($i=0 ; $i<$result->num_rows; $i++){
			$row2 = $result->fetch_object();
			if(($row2->order1 > 1)&&($row2->order2 == 0)){
				$this->content .= $menu->display() . "\n";
			}
			if ($row2->order2 ==0){
				$menu = new Menu($row2->title , $row2->href);
			} else{
				$submenu = new Menu($row2->title,$row2->href);
				$menu->addMenu($row2->id , $submenu);
			}
		}
		$this->content .= $menu->display() . "\n";
		$this->content .= "</ul>\n";
		/*
		$this->content = "<ul>\n";
		$menu = new Menu("학교소개" , "http://www.2woo.net/index.php?option=page&code=intro");
		$submenu = new Menu("학교장 인삿말", "http://www.2woo.net/index.php?option=page&code=intro");
		$menu->addMenu("greeting" , $submenu);
		$submenu = new Menu("교직원 소개", "http://www.2woo.net/index.php?option=page&code=teacher");
		$menu->addMenu("teacher" , $submenu);
		$this->content .= $menu->display() . "\n";

		$menu = new Menu("교육과정" , "http://www.2woo.net/index.php?option=page&code=curriculum");
		$this->content .= $menu->display() . "\n";
		$this->content .= "</ul>\n";
		*/
		$this->content .= $row->content;
	}
	
	public function display($code) {
		$this->makeupPage($code);
		$this->displayDoctype();
		echo "<html>\n";
		$this->displayHead();
		$this->displayBody();
		echo "</html>";
	}
	
	private function displayDoctype(){
		if ($this->doctype == "html4.01_strict"){
			echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>\n";
		} elseif ($this->doctype == "html4.01_trans"){
			echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>\n";
		} elseif ($this->doctype == "xhtml1.0_strict"){
			echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n";
		} elseif ($this->doctype == "xhtml1.0_trans"){
			echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
		} elseif ($this->doctype == "xhtml1.1"){
			echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>\n";
		} elseif ($this->doctype == "html5"){
			echo "<!DOCTYPE HTML>\n";
		}
	}
	
	private function displayHead() {
		echo "\t<head>\n";
		$this->displayMetaTag();
		$this->displayExternalCSS();
		echo "\t\t<title>" . $this->title . "</title>\n";
		$this->displayExternalScript();
		$this->displayInnerStyle();
		$this->displayInnerScript();
		echo "\t</head>\n";	
	}
	
	//ExternalCSS
	private function displayExternalCSS(){
		foreach ($this->externalCSS as $single){
			echo "<link rel='stylesheet' type='text/css' href='" . $single . "' />\n";
		}
	}	
	
	public function addExternalCSS($name){
		$this->externalCSS[] = $name;
	}
	
	//ExternalScript
	private function displayExternalScript(){
		foreach ($this->externalScript as $single){
			echo "<script type='text/javascript' src='" . $single . "'></script>\n";
		}
	}
	
	public function addExternalScript($JS){
		$this->externalScript[] = $JS;
	}
	
	//InnerStyle
	private function displayInnerStyle() {
		if ($this->innerStyle != ""){
			echo "<style>\n";
			echo $this->innerStyle;
			echo "</style>\n";
		}
	}
	
	//InnerScript
	private function displayInnerScript(){
		if ($this->innerScript != ""){
			echo "<script type='text/javascript'>\n";
			echo $this->innerScript;
			echo "</script>\n";
		}
	}
	
	//Meta
	private function displayMetaTag(){
		echo "<meta http-equiv='content-type' content='text/html;charset=utf-8'/>\n"; 
		foreach ($this->meta as $single){
			echo "<meta " . $single["type"] . "=\"" . $single["value"] . "\" content=\"" . $single["content"] . "\" />\n ";
		}
	}
	
	public function addMeta($type, $value, $content){
		$this->meta[] = array("type"=>$type, "value"=>$value, "content"=>$content);
	}

	
	private function displayBody() {
		echo "\t<body>\n";
		echo $this->content;
		echo "\n\t</body>\n";	
	}
}

?>